const http            = require("http");
const url             = require("url");
const socket          = require('./src/socket'); // socket
const router          = require('./src/router');
const handlers = require('./src/handlers');

const handle = {};

handle['/'] = handlers.viewRealtimeLog; // 查看实时反馈
handle['/list'] = handlers.showList; // 展示存储的数据
handle['/report'] = handlers.report; // 接收上报

const PORT = process.env.PORT || 9999;

const server = http.createServer(function (request, response) {
	const pathname = url.parse(request.url).pathname;
	console.log(`pathname: ${pathname}`);
	router(handle, pathname, response, request, socket);
});

socket.init( server );
server.listen(PORT);

console.log("Server has started.", `http://localhost:${PORT}/`);
